//
//  GMainView.m
//  helpinrain
//
//  Created by Gang.Wang on 12-7-26.
//  Copyright (c) 2012年 Zhuhai Yuike Technology Co., Ltd. All rights reserved.
//

#import "GMainView.h"
#import "mcf.h"
#import "GSheetXmlModel.h"
#import "GSecondViewItemView.h"

@interface GMainView ()
{
    GFriestLevelView * firstLevelView_;
    GSecondLevelView * secondLevelView_;
    
    GThridLevelView * thridLevelView_;
}

@property (nonatomic, strong) GFriestLevelView * firstLevelView;
@property (nonatomic, strong) GSecondLevelView * secondLevelView;

@property (nonatomic, strong) GThridLevelView * thridLevelView;

- (void) privateInit;

@end


@implementation GMainView

@synthesize firstLevelView = firstLevelView_;
@synthesize secondLevelView = secondLevelView_;

@synthesize thridLevelView = thridLevelView_;


- (void) dealloc
{
    YRelease(firstLevelView_)
    YRelease(secondLevelView_)
    
    YRelease(thridLevelView_)
    
    [super dealloc];
}

- (id) init
{
    self = [super init];
    if (self) 
    {
        [self privateInit];
    }
    
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self privateInit];
    }
    return self;
}

- (void) privateInit
{
    if (self.bInited) 
    {
        return;
    }
    
    GFriestLevelView * temp_firstLevel = [[GFriestLevelView alloc] init];
    [temp_firstLevel setDelegate:self];
    self.firstLevelView = temp_firstLevel;
    YRelease(temp_firstLevel)
    [self addSubview:self.firstLevelView];
    
    [self reAdjustLayout];
    self.bInited = TRUE;
}


- (void) reAdjustLayout
{
    [self.firstLevelView setFrame:self.bounds];
    [self.secondLevelView setFrame:self.bounds];
    [self.thridLevelView setFrame:self.bounds];
}

#pragma mark - GFriestLevelViewDelegate
- (void) openSheet:(NSString *)sheetName
{
    if ([NSString isNilOrEmpty:sheetName]) 
    {
        return;
    }
    
    GSheetXmlModel * sheet = [[GSheetXmlModel alloc] initWithXmlLocalPath:[NSURL fileURLWithPath:[YPath Combine:kResource combinePath:sheetName]]];
    if (!sheet) 
    {
        return;
    }
    
    
    YHIDEVIEW(self.firstLevelView);
    
    [self.secondLevelView removeFromSuperview];
    YRelease(self.secondLevelView);
    
    GSecondLevelView * temp_secondView = [[GSecondLevelView alloc] init];
    [temp_secondView setDelegate:self];
    self.secondLevelView = temp_secondView;
    YRelease(temp_secondView)
    
    [self addSubview:self.secondLevelView];
    [self bringSubviewToFront:self.secondLevelView];
    [self.secondLevelView setSheet:sheet];
    [self reAdjustLayout];
    [self.secondLevelView reAdjustLayout];

    // open magazine
}


#pragma mark - GSecondLevelViewDelegate
- (void) openSection:(id)section
{
    if (!section) 
    {
        return;
    }
    
    [self.thridLevelView removeFromSuperview];
    YRelease(self.thridLevelView)
    
    GThridLevelView * temp_thridView = [[GThridLevelView alloc] init];
    [temp_thridView setDelegate:self];
    self.thridLevelView = temp_thridView;
    YRelease(temp_thridView)
    
    [self.thridLevelView setSection:section];
    [self addSubview:self.thridLevelView];
    
    [self bringSubviewToFront:self.thridLevelView];
    
    YHIDEVIEW(self.secondLevelView);
    
    [self reAdjustLayout];
    
    
}

- (void) closeSecondLevelView:(id)obj
{
    [self.secondLevelView removeFromSuperview];
    YRelease(self.secondLevelView);
    
    YSHOWVIEW(self.firstLevelView);
}

#pragma mark - GThridLevelViewDelegate
- (void) exitThridLevelView:(id)obj
{
    [self.thridLevelView removeFromSuperview];
    YRelease(self.thridLevelView)
    
    YSHOWVIEW(self.secondLevelView)
}

@end
